<?PHP

class PageVersion
{
    private $_pagetask ;
    private $_version_number ;
    private $_row ;

    function __construct( $pgtask, $version_num = 0 ) {
        $this->_pagetask = $pgtask ;
        $this->_version_number = $version_num ;
    }

/*
    private function initFromPageVersionId( $id ) {
        global $DpDb;
        $this->_page_version_id = $id ;
        $sql = "SELECT * FROM pageversions
                WHERE pageversionid = $id" ;
        this->_$row = $DpDb->SqlOneObject( $sql ) ;
    }

    private function initFromPageTaskId( $id ) {
        global $DpDb;
        $sql = "SELECT * FROM pageversions
                WHERE pagetaskid = $id" ;
        $this->_row = $DpDb->SqlOneObject( $sql ) ;
    }
*/

    /*
    private function InitPageVersion( $vno ) {
        global $DpDb;
        $projectid = $this->_pagetask->ProjectId() ;
        $pagecode = $this->_pagetask->PageCode() ;
        $sql = "SELECT * FROM pageversions
                WHERE projectid = '$projectid'
                    AND pagecode = '$pagecode'
                    AND versionnumber = $vno";
        $this->_row  = $DpDb->SqlOneObject( $sql ) ;
    }
    */

    public function SaveText( $text ) {
        global $DpDb;
        global $User;

        $username               = $User->UserName() ;
        $projectid              = $this->_pagetask->ProjectId() ;
        $pagecode               = $this->_pagetask->PageCode() ;
        $taskcode               = $this->_pagetask->TaskCode() ;
        $pagetaskid             = $this->_pagetask->PageTaskId() ;

        if( $this->_version_number > 0 ) {
            $sql = "UPDATE
                        pageversions
                    SET
                        username = '$username',
                        createtime = UNIX_TIMESTAMP()
                    WHERE
                        projectid = '$projectid'
                        AND pagecode = '$pagecode'
                        AND versionnumber =  $this->_version_number\n";
                        
        } 
        else {
            $this->_version_number = $this->_pagetask->MaxVersionNumber() + 1 ;
            // otherwise add a new pageversion
            $sql = "INSERT INTO pageversions
                    (
                        pagetaskid,
                        projectid,
                        pagecode,
                        taskcode,
                        versionnumber,
                        username,
                        createtime
                    ) values (
                         $pagetaskid,
                        '$projectid',
                        '$pagecode',
                        '$taskcode',
                         $this->_version_number,
                        '$username',
                        UNIX_TIMESTAMP()
                    )";

        }
        $DpDb->SqlExecute($sql) == 1
            or die( $sql ) ;
        $this->WriteTextFile( $text ) ;
        $this->_pagetask->RemoveTempText() ;
    }

    public function ProjectId() {
        return $this->_pagetask->ProjectId() ;
    }

    public function PageCode() {
        return $this->_pagetask->PageCode() ;
    }

    public function TaskCode() {
        return $this->_pagetask->TaskCode() ;
    }

    public function PageTaskId() {
        return $this->_pagetask->PageTaskId() ;
    }

    public function VersionNumber() {
        return $this->_version_number ;
    }

    private function WriteTextFile( $text ) {
        $path = $this->FilePath() ;
        file_put_contents( $path, $text);
    }

    private function FilePath() {
        $p = PageVersionPath( $this->ProjectId(),
                              $this->PageCode(),
                              $this->_version_number ) ;
        return $p ;
    }

    public function PageText() {
        $path = $this->FilePath();
        if(empty($path) || (! file_exists($path))) {
            return null ;
                /*
                die( "Project {$this->_projectid}
                      Page {$this->_pagecode}
                      Version {$this->_version_number}
                      No text found at $path." ) ;
              */
        }
        else
            return file_get_contents( $path ) ;
    }

    public function SetFromPath($path) {
        $topath = $this->FilePath() ;
        copy($path, $topath);
    }

    private function IsFile() {
        return file_exists($this->FilePath()) ;
    }

    public function Delete() {
        global $DpDb;
        $path = $this->FilePath();
        if( file_exists( $path ) )
                unlink( $path ) ;

        $sql = "DELETE FROM pageversions
                WHERE projectid = '{$this->ProjectId()}'
                    AND pagecode = '{$this->PageCode()}'
                    AND versionnumber >= {$this->VersionNumber()}";
        $DpDb->SqlExecute($sql) ;
    }

    public function FileSize() {
        if( ! $this->IsFile() )
            return 0 ;
        else
            return filesize($this->FilePath() ) ;
    }
}
?>
